<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>俄罗斯套娃信封问题</title>
</head>
<body>
  <script>
    function fn(envelopes) {
      const n = envelopes.length;
      // w升序排序，如果w相同，h逆序排序
      // 因为w相同时不能互相嵌套，所以w相同时将h逆序排序
      envelopes.sort((e1, e2) => (e1[0] !== e2[0] ? e1[0] - e2[0] : e2[1] - e1[1]));
      // 求height数组，排好序后的h组成的数组
      const height = new Array(n).fill(1);
      for (let i = 0; i < n; i++) {
        height[i] = envelopes[i][1];
      }
      // 求h数组的最长递增子序列
      const dp = new Array(n).fill(1);
      for (let i = 0; i < height.length; i++) {
        for (let j = 0; j < i; j++) {
          if (height[i] > height[j]) {
            dp[i] = Math.max(dp[i], dp[j] + 1);
          }
        }
      }
      return Math.max(...dp)
    }
    console.log(fn([[5, 4], [6, 4], [6, 7], [2, 3]]))
  </script>
</body>
</html>